package com.hhit.collaborative_learning.community;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hhit.collaborative_learning.dao.BBS_ReplyDao;
import com.hhit.collaborative_learning.dao.BBS_ReplyMessageDao;
import com.hhit.collaborative_learning.dao.BBS_TopicDao;
import com.hhit.collaborative_learning.dao.impl.BBS_ReplyDaoImpl;
import com.hhit.collaborative_learning.dao.impl.BBS_ReplyMessageDaoImpl;
import com.hhit.collaborative_learning.dao.impl.BBS_TopicDaoImpl;
import com.hhit.collaborative_learning.utils.DBAccess;
import com.hhit.collaborative_learning.utils.JsonUtil;
import com.hhit.collaborative_learning.utils.ResponseUtil;
import com.mysql.jdbc.StringUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * Servlet implementation class GetTopicAndReplyServlet
 */
@WebServlet("/community/GetTopicAndReplyServlet")
public class GetTopicAndReplyServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public GetTopicAndReplyServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int pagenow=0,pagecount=0,pagenum=0;
		String topic_id = request.getParameter("topic_id");
		String pageNow = request.getParameter("pageNow");
		String pageCount = request.getParameter("pageCount");
		if(!StringUtils.isNullOrEmpty(pageNow)&&!StringUtils.isNullOrEmpty(pageCount)){
			pagenow=Integer.valueOf(pageNow);
			pagecount=Integer.valueOf(pageCount);
			System.out.println("pagecount"+pagecount);
		}
		BBS_ReplyMessageDao rmd=new BBS_ReplyMessageDaoImpl();
		BBS_ReplyDao rd=new BBS_ReplyDaoImpl();
		JSONObject jobj=new JSONObject();
		JSONObject jsonTopic=new JSONObject();
		Connection con = DBAccess.getInstance().getConn();
		try {
			if ("1".equals(pageNow)) {
				pagecount-=1;
				BBS_TopicDao td = new BBS_TopicDaoImpl();
				ResultSet rsTopic = td.query(con, 0, 0, "bt.ID", "= "+topic_id,
						"desc", "bt.CreatedOn");
				td.updateHit(con, " td="+topic_id);
				jsonTopic = JsonUtil.formatRsToJSONObject(rsTopic);
			}
			String find="",cfind="";
			if(!StringUtils.isNullOrEmpty(topic_id)){
				find+=" br.TopicID = "+topic_id+" ";
				cfind=" TopicID = "+topic_id+" ";
			}
			ResultSet rsReply=rd.query(con, (pagenow - 1) * pagecount, (pagenow * pagecount - 1), find, "asc", "br.CreatedOn");
			JSONArray replyArray= JsonUtil.formatRsToJsonArray(rsReply);
			System.out.println("--- "+ replyArray.toString());
			for(Object j:replyArray){
				JSONObject js=(JSONObject) j ;
				int id=(int) js.get("ID");
				ResultSet rsRm=rmd.query(con, 0, 0, "brm.ReplyID = "+id, "asc", "brm.CreatedOn");
				JSONArray rmArray=JsonUtil.formatRsToJsonArray(rsRm);
				js.put("replyMessage", rmArray.toString());
			}
			int reNum=rd.getcount(con, cfind);
			pagenum= (int) Math.ceil(((double)reNum)/((double)pagecount));
			jobj.put("pagenow", pageNow);
			jobj.put("pagenum", pagenum);
			jobj.put("topic", jsonTopic.toString());
			jobj.put("reply", replyArray.toString());
			ResponseUtil.write(response, jobj.toString());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBAccess.getInstance().close(con, null, null);
		}

	}

}
